﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class cms_default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // FilmGridView.DataBind();

    }

    // After events ...
    protected void FilmDetailsOverview_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        FilmGridView.DataBind();
    }

    protected void FilmDetailsOverview_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        FilmGridView.DataBind();
    }

    protected void FilmDetailsOverview_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
    {
        FilmGridView.DataBind();
    }

    // All ongoing events ....
    protected void FilmDetailsOverview_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        CheckBoxList KijkwijzerShow = (CheckBoxList)FilmDetailsOverview.FindControl("KijkwijzerShow");
        
        kijkwijzer_film.DeleteCommand = "DELETE FROM film_kijkwijzer WHERE filmfilm_id = " + FilmGridView.SelectedValue + "";
        kijkwijzer_film.Delete();

                
        for (int i = 0; i < KijkwijzerShow.Items.Count; i++)
        {
            kijkwijzer_film.InsertCommand = "INSERT INTO film_kijkwijzer(filmfilm_id, kijkwijzerkijkwijzer_id) VALUES (" + FilmGridView.SelectedValue + ", " + KijkwijzerShow.Items[i].Value + ")";
        
            if (KijkwijzerShow.Items[i].Selected == true)
            {
                kijkwijzer_film.Insert();
            }

        }

        CheckBoxList GenreShow = (CheckBoxList)FilmDetailsOverview.FindControl("GenreShow");

        genre_film.DeleteCommand = "DELETE FROM film_genre WHERE filmfilm_id = " + FilmGridView.SelectedValue + "";
        genre_film.Delete();
       
        for (int i = 0; i < GenreShow.Items.Count; i++)
        {
            genre_film.InsertCommand = "INSERT INTO film_genre(filmfilm_id, genregenre) VALUES (" + FilmGridView.SelectedValue + ", '" + GenreShow.Items[i].Value + "')";

            if (GenreShow.Items[i].Selected == true)
            {
                genre_film.Insert();
            }
        }
    }

    protected void Film_Details_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
        string sID = e.Command.Parameters["@Identity"].Value.ToString();
        //Button1.Text = sID;

        CheckBoxList KijkwijzerShow = (CheckBoxList)FilmDetailsOverview.FindControl("KijkwijzerShow");


        for (int i = 0; i < KijkwijzerShow.Items.Count; i++)
        {
            kijkwijzer_film.InsertCommand = "INSERT INTO film_kijkwijzer(filmfilm_id, kijkwijzerkijkwijzer_id) VALUES (" + sID + ", " + KijkwijzerShow.Items[i].Value + ")";

            if (KijkwijzerShow.Items[i].Selected == true)
            {
                kijkwijzer_film.Insert();
            }

        }

        CheckBoxList GenreShow = (CheckBoxList)FilmDetailsOverview.FindControl("GenreShow");

        for (int i = 0; i < GenreShow.Items.Count; i++)
        {
            genre_film.InsertCommand = "INSERT INTO film_genre(filmfilm_id, genregenre) VALUES (" + sID + ", '" + GenreShow.Items[i].Value + "')";

            if (GenreShow.Items[i].Selected == true)
            {
                genre_film.Insert();
            }
        }

    }

    protected void FilmDetailsOverview_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {

    }
    
    protected void FilmDetailsOverview_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
        kijkwijzer_film.DeleteCommand = "DELETE FROM film_kijkwijzer WHERE filmfilm_id = " + FilmGridView.SelectedValue + "";
        kijkwijzer_film.Delete();
        genre_film.DeleteCommand = "DELETE FROM film_genre WHERE filmfilm_id = " + FilmGridView.SelectedValue + "";
        genre_film.Delete();
    }

    protected void FilmGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        FilmDetailsOverview.ChangeMode(DetailsViewMode.ReadOnly);
        string select_id = FilmGridView.DataKeys[FilmGridView.SelectedIndex]["film_id"].ToString();
        Film_Select.SelectParameters.Clear();
        Film_Select.SelectParameters.Add("film_id", select_id);
        FilmDetailsOverview.DataBind();
    }

    // useless code
    //protected void FilmDetailsOverview_DataBound(object sender, EventArgs e)
    //{
    //    if (FilmDetailsOverview.CurrentMode == DetailsViewMode.Edit)
    //    {
    //        foreach (DetailsViewRow dvr in FilmDetailsOverview.Rows)
    //        {
    //            if (dvr.Cells.Count.Equals(2) && dvr.Cells[1].HasControls())
    //            {
    //                Control ctrl = dvr.Cells[1].Controls[0];

    //                if (ctrl is TextBox)
    //                {
    //                    TextBox txt = ctrl as TextBox;

    //                    txt.TextMode = TextBoxMode.MultiLine;
    //                    txt.Width = 750;
    //                    txt.Rows = 1;
    //                }
    //            }
    //        }
    //    }
    //    if (FilmDetailsOverview.CurrentMode == DetailsViewMode.Insert)
    //    {
    //        foreach (DetailsViewRow dvr in FilmDetailsOverview.Rows)
    //        {
    //            if (dvr.Cells.Count.Equals(2) && dvr.Cells[1].HasControls())
    //            {
    //                Control ctrl = dvr.Cells[1].Controls[0];

    //                if (ctrl is TextBox)
    //                {
    //                    TextBox txt = ctrl as TextBox;

    //                    txt.TextMode = TextBoxMode.MultiLine;
    //                    txt.Width = 750;
    //                    txt.Rows = 1;
    //                }
    //            }
    //        }
    //    }
    //}


    protected void Button1_Click(object sender, EventArgs e)
    {
        FilmDetailsOverview.Visible = true;
        FilmDetailsOverview.ChangeMode(DetailsViewMode.Insert);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        FilmDetailsOverview.ChangeMode(DetailsViewMode.ReadOnly);
        FilmGridView.SelectedIndex = -1;
    }
    protected void KijkwijzerShow_Databound(object sender, EventArgs e)
    {

        CheckBoxList KijkwijzerShow = (CheckBoxList)FilmDetailsOverview.FindControl("KijkwijzerShow");
        foreach (ListItem item in KijkwijzerShow.Items)
        {
            item.Text = string.Format("<img src=\"{0}\" style=\"background-color: white\" height=\"32px\" width=\"32px\" />", item.Text);
            //item.Attributes.Add("style", "background-color: white"); 
        }
    }

    protected void KijkwijzerFilm_Databound(object sender, EventArgs e)
    {

        CheckBoxList KijkwijzerFilm = (CheckBoxList)FilmDetailsOverview.FindControl("KijkwijzerFilm");
        foreach (ListItem item in KijkwijzerFilm.Items)
        {
            item.Text = string.Format("<img src=\"{0}\" style=\"background-color: white\" height=\"32px\" width=\"32px\" />", item.Text);
            //item.Attributes.Add("style", "background-color: white"); 
        }
    }

    protected void KijkwijzerCheck_Databound(object sender, EventArgs e)
    {

        CheckBoxList KijkwijzerShow = (CheckBoxList)FilmDetailsOverview.FindControl("KijkwijzerShow");
        CheckBoxList KijkwijzerFilm = (CheckBoxList)FilmDetailsOverview.FindControl("KijkwijzerFilm");

        foreach (ListItem item in KijkwijzerShow.Items)
        {
            item.Text = string.Format("<img src=\"{0}\" style=\"background-color: white\" height=\"32px\" width=\"32px\" />", item.Text);
            //item.Attributes.Add("style", "background-color: white"); 

            foreach (ListItem item2 in KijkwijzerFilm.Items)
            {
                if (item.Text == item2.Text)
                {
                    item.Selected = true;
                }
            }
        }
    }

    protected void GenreCheck_Databound(object sender, EventArgs e)
    {

        CheckBoxList GenreShow = (CheckBoxList)FilmDetailsOverview.FindControl("GenreShow");
        CheckBoxList GenreFilm = (CheckBoxList)FilmDetailsOverview.FindControl("GenreFilm");

        foreach (ListItem item in GenreShow.Items)
        {
            foreach (ListItem item2 in GenreFilm.Items)
            {
                if (item.Text == item2.Text)
                {
                    item.Selected = true;
                }
            }
        }
    }

    protected void Upload_Image(object sender, EventArgs e)
    {
        FileUpload UploadImages = (FileUpload)FilmDetailsOverview.FindControl("UploadImages");
        Label uploadLabel = (Label)FilmDetailsOverview.FindControl("uploadLabel");
        Label LinkLabel = (Label)FilmDetailsOverview.FindControl("LinkLabel");
        TextBox urlTextBox = (TextBox)FilmDetailsOverview.FindControl("urlTextBox");
        FileUpload UploadImages2 = (FileUpload)FilmDetailsOverview.FindControl("UploadImages2");
        Label uploadLabel2 = (Label)FilmDetailsOverview.FindControl("uploadLabel2");
        Label LinkLabel2 = (Label)FilmDetailsOverview.FindControl("LinkLabel2");
        TextBox urlTextBox2 = (TextBox)FilmDetailsOverview.FindControl("urlTextBox2");

        if (UploadImages.HasFile)
        {

            //  empty label
            uploadLabel.Text = "";

            //  get extentie
            string fileExt = System.IO.Path.GetExtension(UploadImages.FileName);

            //  check if fileextension is supported
            if (fileExt != ".jpg" & fileExt != ".bmp" & fileExt != ".jpeg" & fileExt != ".png" & fileExt != ".gif")
            {
                uploadLabel.Text = "Het bestandstype word niet ondersteund.";
            }
            else
            {

                try
                {
                    String filename = UploadImages.FileName;
                    String path = Server.MapPath("~") + "\\resources\\images\\movies\\" + filename;
                    String pathwebsite = "/debioscoop/resources/images/movies/" + filename;
                    //     if file exists add a 1 behind the name and check again. 
                    while (System.IO.File.Exists(path))
                    {

                        String name = filename.Replace(fileExt, "");

                        String newFilename = name + "i" + fileExt;
                        filename = newFilename;
                    }


                    UploadImages.SaveAs(path);

                    LinkLabel.Text = pathwebsite;

                    uploadLabel.Text = "Het bestand is succesvol upgeload.";

                }
                catch (Exception ex)
                {
                    uploadLabel.Text = "ERROR: " + ex.Message.ToString();
                }
            }
        }
    }
    protected void Upload_Image2(object sender, EventArgs e)
    {
        FileUpload UploadImages2 = (FileUpload)FilmDetailsOverview.FindControl("UploadImages2");
        Label uploadLabel2 = (Label)FilmDetailsOverview.FindControl("uploadLabel2");
        Label LinkLabel2 = (Label)FilmDetailsOverview.FindControl("LinkLabel2");
        TextBox urlTextBox2 = (TextBox)FilmDetailsOverview.FindControl("urlTextBox2");

        if (UploadImages2.HasFile)
        {

            //  empty label
            uploadLabel2.Text = "";

            //  get extentie
            string fileExt = System.IO.Path.GetExtension(UploadImages2.FileName);

            //  check if fileextension is supported
            if (fileExt != ".jpg" & fileExt != ".bmp" & fileExt != ".jpeg" & fileExt != ".png" & fileExt != ".gif")
            {
                uploadLabel2.Text = "Het bestandstype word niet ondersteund.";
            }
            else
            {

                try
                {
                    String filename = UploadImages2.FileName;
                    String path = Server.MapPath("~") + "\\resources\\images\\movies\\" + filename;
                    String pathwebsite = "/debioscoop/resources/images/movies/" + filename;
                    //     if file exists add a 1 behind the name and check again. 
                    while (System.IO.File.Exists(path))
                    {

                        String name = filename.Replace(fileExt, "");

                        String newFilename = name + "i" + fileExt;
                        filename = newFilename;
                    }


                    UploadImages2.SaveAs(path);

                    LinkLabel2.Text = pathwebsite;

                    uploadLabel2.Text = "Het bestand is succesvol upgeload.";

                }
                catch (Exception ex)
                {
                    uploadLabel2.Text = "ERROR: " + ex.Message.ToString();
                }
            }
        }
    }

    protected void CheckLength(String id, int Maxlength)
    {
        TextBox textbox = (TextBox)FilmDetailsOverview.FindControl(id);
        String tekst = textbox.Text;
        int lenght = tekst.Length;

        if (lenght > Maxlength)
        {
            // prevent uploading
        }
    }
}